﻿var list = {
    gualitySchemeNumbering: '',
    gualitySchemeName: '',
    gualitySchemeState: '',
    gualityGroupId: '',
    GualityProjectList:[]
}
layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;
    form.render();
   
    // 表单提交事件
    form.on('submit(demo-login)', function (data) {
        var field = data.field;
        console.log(field)
        var checkStatus = table.checkStatus('carriertable');
        var selectedIds = checkStatus.data.map(function (item) { return item.gualityProjectId; });
        console.log(selectedIds)
        if (selectedIds.length === 0) {
            layer.msg('请至少选择一条要添加的质检项目', { icon: 2 });
            return;
        }
        list.gualitySchemeNumbering = $("#gualitySchemeNumbering").val();
        list.gualitySchemeName = $("#gualitySchemeName").val();
        list.gualitySchemeState = $("#gualitySchemeState").val();
        list.gualityGroupId = $("#gualityGroupId").val();
        list.GualityProjectList = selectedIds;
        console.log(list)
        $.ajax({
            url: '/GualityScheme/GualitySchemeAddShow',
            type: 'post',
            data: { data: JSON.stringify(list), token: localStorage.getItem("token") },
            success: function (res) {
                var s = JSON.parse(res)
                console.log(res)
                if (s.data >= 1) {
                    layer.alert("添加成功");
                    setTimeout(function () {
                        location.reload();
                    }, 2000);
                }
                else {
                    layer.alert("添加失败");
                }
            }
        })
        return false; // 阻止默认 form 跳转
    });
    $(function () {
        Show();
    })
    function Show() {
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['exports'],
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'gualityProjectName', title: '质检项目名称' },
                { field: 'gualityProjectNumbering', title: '质检项目编号' },
                { field: 'gualityProjectTool', title: '质检方法' },
                { field: 'gualityProjectMethod', title: '质检工具' },
                { field: 'gualityProjectType', title: '质检类型' },
                { field: 'gualityProjectStandardValue', title: '标准值' },
                {
                    field: 'addTime', title: '添加时间', templet: function (d) {
                        return formatTime(d.addTime); // 调用自定义的时间格式转换函数
                    }
                },
                { field: 'addName', title: '创建人' },
                { field: 'gualityProjectRemark', title: '备注' },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.gualityProjectId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.gualityProjectId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }

                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        // 创建表格实例
        $.ajax({
            url: "/GualityProject/GualityProjectShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 100
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
        
    }
    // 定义时间格式转换函数
    function formatTime(timeStamp) {
        if (timeStamp) {
            var date = new Date(timeStamp);
            var year = date.getFullYear();
            var month = ('0' + (date.getMonth() + 1)).slice(-2);
            var day = ('0' + date.getDate()).slice(-2);
            var hour = ('0' + date.getHours()).slice(-2);
            var minute = ('0' + date.getMinutes()).slice(-2);
            var second = ('0' + date.getSeconds()).slice(-2);
            return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        }
        return "";
    }
    
    
});
layui.use('form', function () {
    var form = layui.form;
    //下拉框
    $.ajax({
        url: "/GualityGroup/GualityGroupSelect",
        type: "get",
        data: { token: localStorage.getItem("token") },
        success: function (data) {
            var data = JSON.parse(data);
            console.log(data)
            var options = '<option value="">请选择质检组</option>'; // 添加默认选项
            $.each(data.data, function (index, item) {
                console.log(item)
                options += '<option value="' + item.gualityGroupId + '">' + item.gualityGroupName + '</option>';
            });
            $("#gualityGroupId").html(options);
            console.log(options)
            console.log($("#gualityGroupId").val())
            form.render('select'); // 重新渲染下拉框
        },
        error: function () {
            console.log("获取数据失败");
            // 可以考虑显示一个错误消息给用户
        }
    });
});
